package edu.ccut.saturn.studio.logic;

import java.io.File;
import java.util.Map;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Document;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.search.dialog.TwoCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnEndNode;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnInformationNodeInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnStartNodeInLogic;

public class LogicWizardPage extends SaturnWizardPage {
	private Text textDec;
	private Text textComponent;
	private Text textKey;
	private Text textNodeName;
	private Text textFileName;
	private Shape shape = null;

	private static final String LABEL_FILE_NAME = Messages
			.getString("LogicWizardPage.filename"); //$NON-NLS-1$
	private static final String LABEL_NODE_NAME = Messages
			.getString("LogicWizardPage.name"); //$NON-NLS-1$
	private static final String LABEL_KEY = Messages
			.getString("LogicWizardPage.keyvalue"); //$NON-NLS-1$
	// private static final String LABEL_COMPOENET = Messages
	// .getString("LogicWizardPage.component"); //$NON-NLS-1$
	private static final String LABEL_DEC = Messages
			.getString("LogicWizardPage.description"); //$NON-NLS-1$
	public static final String FILE_TYPE = ".logic";
	private static final String START_NODE_VALUE = Messages
			.getString("LogicWizardPage.start"); //$NON-NLS-1$
	private static final String END_NODE_VALUE = Messages
			.getString("LogicWizardPage.end"); //$NON-NLS-1$
	// private static final String TITLE_NAME = Messages
	// .getString("LogicWizardPage.ComponentDescription"); //$NON-NLS-1$

	public static final String LOGIC_WIZARD_FILE_NAME = "LOGIC_FILE_NAME";

	private static final String TEMPLATE_SATURNINFO_NAME = "saturnInfo";
	private static final String TEMPLATE_SATURNSTART_NAME = "saturnStart";
	private static final String TEMPLATE_SATURNEND_NAME = "saturnEnd";
	public static final String TEMPLATE_SATURNCOMPONENT_NAME = "saturnComponent";

	public LogicWizardPage() {
		super();
	}

	@Override
	public void initalValues(Map<String, Object> values) {
		SaturnInformationNodeInLogic info = iniInfoNode();
		SaturnStartNodeInLogic start = iniStartNode();
		SaturnEndNode end = iniEndNode();

		values.put(LOGIC_WIZARD_FILE_NAME, this.textFileName.getText().trim());

		values.put(TEMPLATE_SATURNINFO_NAME, info);
		values.put(TEMPLATE_SATURNSTART_NAME, start);
		values.put(TEMPLATE_SATURNEND_NAME, end);
		values.put(TEMPLATE_SATURNCOMPONENT_NAME, this.shape);
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		setControl(container);
		setTitle(Messages.getString("LogicWizardPage.ComponentInformation")); //$NON-NLS-1$

		final Label label = new Label(container, SWT.NONE);
		label.setText(LABEL_FILE_NAME);

		textFileName = new Text(container, SWT.BORDER);
		textFileName.setTextLimit(50);
		textFileName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
				textKey.setText(getComponentKey(textFileName.getText().trim())
						+ ".logic");
			}
		});
		final GridData gd_textFileName = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		textFileName.setLayoutData(gd_textFileName);
		new Label(container, SWT.NONE);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText(LABEL_NODE_NAME);

		textNodeName = new Text(container, SWT.BORDER);
		textNodeName.setTextLimit(50);
		textNodeName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textNodeName = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		textNodeName.setLayoutData(gd_textNodeName);
		new Label(container, SWT.NONE);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setText(LABEL_KEY);

		textKey = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textKey = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textKey.setLayoutData(gd_textKey);
		new Label(container, SWT.NONE);

		final Label label_3 = new Label(container, SWT.NONE);
		label_3.setText(Messages.getString("LogicWizardPage.Component")); //$NON-NLS-1$

		textComponent = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_textComponent = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		textComponent.setLayoutData(gd_textComponent);

		final Label browse = new Label(container, SWT.NONE);
		browse.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
//				OneCompontentSearchDialog searchDialog = new OneCompontentSearchDialog(
//						Display.getDefault().getShells()[0], "logic",
//						ProjectUtil.getProject(getSelection()));
				String  suffixes [] = {"logic","component"};
				TwoCompontentSearchDialog searchDialog = new TwoCompontentSearchDialog(
						Display.getDefault().getShells()[0],suffixes);
				if (searchDialog.open() == Window.OK) {
					SearchResult expression = (SearchResult) searchDialog
							.getFirstResult();
					textComponent.setText(expression.getKey());
					Document doc = expression.getDocument();
					String tempPath = expression.getFileName() + "--"
							+ expression.getKey();
					shape = NodeChooseFactory.getInstance(tempPath, doc);
					shape.setKey(expression.getKey());

				}
			}
		});
		browse.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browse.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				browse.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		browse.setImage(Activator.getImage(Images.AMPLIFY_PLUS));

		final Label label_4 = new Label(container, SWT.NONE);
		label_4.setText(LABEL_DEC);

		textDec = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		textDec.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_textDec.heightHint = 76;
		textDec.setLayoutData(gd_textDec);

		validate();
	}

	private SaturnInformationNodeInLogic iniInfoNode() {
		SaturnInformationNodeInLogic infoNode = new SaturnInformationNodeInLogic();
		infoNode.setDescription(textDec.getText().trim());
		infoNode.setKey(textKey.getText().trim());
		infoNode.setName(textNodeName.getText().trim());
		return infoNode;
	}

	private SaturnStartNodeInLogic iniStartNode() {
		SaturnStartNodeInLogic startNode = new SaturnStartNodeInLogic();
		startNode.setName(START_NODE_VALUE);
		return startNode;
	}

	private SaturnEndNode iniEndNode() {
		SaturnEndNode endNode = new SaturnEndNode();
		endNode.setName(END_NODE_VALUE);
		return endNode;
	}

	// 输入值校验方法
	public boolean validate() {
		String fileName = textFileName.getText().trim();
		String logicName = textNodeName.getText().trim();
		String description = textDec.getText().trim();

		setPageComplete(false);

		if (new File(getFullPath() + "/" + fileName + ".logic").exists()) {
			setErrorMessage(Messages.getString("LogicWizardPage.FilenameExist")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(fileName)) {
			setErrorMessage(Messages.getString("LogicWizardPage.FileNameLimit")); //$NON-NLS-1$
			return false;
		}
		if (SaturnValidation.isGB2312(fileName)) {
			setErrorMessage(Messages.getString("LogicWizardPage.IsGB2312")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(fileName)) {
			setErrorMessage(Messages
					.getString("LogicWizardPage.FilenameLimit1")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(logicName)) {
			setErrorMessage(Messages.getString("LogicWizardPage.NameLimit")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(logicName)) {
			setErrorMessage(Messages.getString("LogicWizardPage.NameLimit1")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(description)) {
			setDescription(Messages
					.getString("LogicWizardPage.InformationEmpty")); //$NON-NLS-1$
		}

		setErrorMessage(null);
		setPageComplete(true);
		return true;

	}
}
